;(function(){
    var c_city=Utils.getCookie("currentCity");//获取cookie中的地址信息
    var l_city=Utils.getCurrentCity("currentCity");//获取当前的城市地址
    var curr_city=document.getElementById("currentCity");//找到currentCity对象
    var shop_u_id=Utils.getQueryString("shopId");//通过url
    var shop_c_id=Utils.getCookie("shopId");//通过cookie
    var user_u_id=Utils.getQueryString("userId");//通过url
    var user_c_id=Utils.getCookie("userId");//通过cookie
    var productId=Utils.getQueryString("productId");//当前产品名
    var userId=0;
    if(user_c_id||user_u_id){
        userId=parseInt(user_c_id||user_u_id);
    }
    var hasLogin=Utils.getCookie("hasLogin");//用户已登录
    var url=Utils.reqUrl,
        asyn=true,
        type="post";
    function getCurr_city(){
        var  reqUrl='http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js';
        function callback(){
            var city=remote_ip_info.city;
            Utils.setCookie("currentCity",city);
            curr_city.innerHTML=city;
        }
        //如果已经定位过,直接设置为已定位的，否则重新自动定位
        if(l_city||c_city){
            var city=l_city||c_city;
            Utils.setCookie("currentCity",city);
            curr_city.innerHTML=city;    
        }else{
            $.getScript(reqUrl,callback);
        }
        setIndexData(curr_city.innerHTML);
        $(".changecity a").on("click",function(){
            location.href="./address.html?currentCity="+curr_city.innerHTML;
        });
    }
    //定位城市
    getCurr_city(setIndexData);
    function setIndexData(current_city){
        var address;
        if(c_city||l_city){
            address=c_city||l_city;
        }else{
            address=current_city;
        }
        /**
         *----页面跳转
         **/
        function jump_page(ele){
            var ele=ele;
            $(ele).on("click",function(){
                var links=$(this).attr("data-links");
                location.href="./"+links+".html?currentCity="+address;
            });   
        }
        jump_page(".jump_links");
        
        //获取商品详情
        var requestData=JSON.stringify({
            "code":"getProductDetail",
            "version":"1.0",
            "data":{
                "productId":productId,
                "userId":userId
            }
        });
        //获取商品类别，包括大类跟小类
        var kid_p_type=function(proId){
            var proId=parseInt(proId);
            return JSON.stringify({
                 "code":"getPdOrServTypeList",
                 "version":"1.0",
                 "data":{
                     "parentServiceId":0,
                     "type":"PRODUCT",
                     "parentPdTypeId":proId
                 }
            });
        }
        //ajax获取数据
        function getData(url,requestData,call_func,asyn,type){
            console.log("请求参数"+requestData)
            $.ajax({
                url:url,
                type:type,
                datatype:"json",
                contentType:"application/json",
                data:requestData,
                async:asyn,
                success:function(data){
                    console.log(data);
                    call_func(data);
                },
                error:function(msg){
                    alert(msg);
                }   
            });        
        }
        //获取商品详情信息
        getData(url,requestData,call_func_detail,asyn,type);
       
        //进入页面后判断是否登录，
        function setCollect(){
            if(!hasLogin){//未登录(默认显示未收藏的标志)
                $(".collect-log").find(".img1").addClass("un-collect").removeClass("has-collect");
            }
            clickCollect();
        }
        //商家位置地图
        function setMapAddr(locX,locY){
                var locX=locX;
                var locY=locY;
                var map=new BMap.Map("map-pic");
                map.centerAndZoom(new BMap.Point(locX,locY),15);
                map.addControl(new BMap.NavigationControl());        // 添加平移缩放控件
                map.addControl(new BMap.ScaleControl());             // 添加比例尺控件
                map.addControl(new BMap.OverviewMapControl());       //添加缩略地图控件
                map.enableScrollWheelZoom(true);
                var geolocation = new BMap.Geolocation();
                //设置标注的图标
                var icon = new BMap.Icon("../images/loc3.png",new BMap.Size(60,60));
                var marker1 = new BMap.Marker(new BMap.Point(locX,locY),{icon:icon});        
                map.addOverlay(marker1);
        }
        /**
         *点击收藏
         **/                
        function clickCollect(){
            $(".collect-log").on("click",function(){
                if(hasLogin){
                    var coll_img=$(this).find(".img1").attr("class");
                    if(coll_img.indexOf("un-collect")>-1){//收藏
                        $(this).find(".img1").addClass("has-collect").removeClass("un-collect");
                        $(".collect-log .count1").html("已收藏");
                        collectData=JSON.stringify({
                            "code":"userCollect",
                            "data":{
                             "productId":productId,
                             "userId":userId
                            },
                            "version":"1.0"
                        });
                        getData(url,collectData,call_func_coll_result,asyn,type);
                    }else{//取消收藏
                        $(this).find(".img1").addClass("un-collect").removeClass("has-collect");
                        $(".collect-log .count1").html("收藏");
                        cancelData=JSON.stringify({
                            "code":"cancelCollect",
                            "data":{
                                "productIds":[productId],
                                "userId":userId
                            },
                            "version":"1.0"
                        });
                        getData(url,cancelData,call_func_coll_result,asyn,type);
                    }    
                }else{
                    $(".login").show(200);
                    $("#loginBox").show(500);
                    var phoneNO;
                    var requestData="";
                    var userNo= Utils.getCookie("userNo");
                    var pwd= Utils.getCookie("password");
                    if(pwd){
                        $("[name='checkbox']").attr("checked", "true");
                    }
                    if (userNo) {//用户名存在的话就把用户名填充到用户名文本框
                        $(".login-name .login-input").val(userNo);
                    }
                    if (pwd) {//密码存在的话把密码填充到密码文本框
                        $(".login-pwd .login-input").val(pwd);
                    }
                    var password=$(".login-pwd .login-input").val();
                    
                    //手动输入加密
                    $(".login-pwd .login-input").change(function(){
                          password=$(".login-pwd .login-input").val();
                          password=hex_md5(password);
                    });
                    $(".loginIn").on("click",function(){
                        phoneNO=$(".login-name .login-input").val();
                        var p_wd=$(".login-pwd .login-input").val();
                         if($.trim(p_wd)=="" || $.trim(phoneNO)==""){
                              $(".error").css("display","block");
                        }else{
                               $(".error").css("display","none");
                        }
                        logindata=JSON.stringify({
                            "code":"login",
                            "version":"1.0",
                            "data":{
                                "phoneNO":phoneNO,
                                "password":password
                            }
                        });
                        getData(url,logindata,handdle_func1,asyn,type);
                    });                    
                    function handdle_func1(data){
                        var userName=data.data.user.userName;//用户名
                        var userId=data.data.user.userId;//用户id
                        var shopId=data.data.seller.shopId;//商家id
                        if(data.msg=="登录成功"){
                            console.log("登录成功");
                            hasLogin=true;//登录成功
                            Utils.setCookie("shopId",shopId);
                            Utils.setCookie("hasLogin",hasLogin);//已登录标志
                            Utils.setCookie("userName",userName);//用户名
                            Utils.setCookie("userId",userId);//用户id
                            if($(".auto-login").attr("checked")){//记住密码
                                Utils.setCookie("userNo",phoneNO);
                                Utils.setCookie("password",password);//登录成功

                            }else{
                                Utils.delCookie("userNo");
                                Utils.delCookie("password");

                            }
                            //登录成功之后，reload当前页面或者跳转到当前的页面
                            location.reload();
                        }else{
                            console.log("登录失败");
                            $(".error").css("display","block");
                            $(".error").html("账号或者密码错误！");
                        }
                    }    
                }
            });
        }
        //关闭登陆窗口    
        $(document).on("click",".closeBtn",function(){
            $(".login").hide(200);
            $("#loginBox").hide(500);
        });
        
        /**
         *收藏或者取消结果
         **/
        function call_func_coll_result(data){
            console.log("结果");
        }
        /**
         *获取商品详情信息---------------------
         **/
        function call_func_detail(data){
            var datas = data.data.product;
            var shopData=data.data.shop;
            var b_type = datas.pdTypeName;//商品类型
            var p_name = datas.productName;//商品名字
            var p_mark = datas.remark;//商品说明
            var p_price = datas.saleprice;//商品价格
            var p_unit = datas.unit;//商品单位
            var p_view = datas.viewCount;//浏览数
            var def_img = datas.fullPath;//默认初始图片
            //将图片转换成大图
            var fullpath=def_img.add_suffix_max();
            console.log(fullpath);
            var isCollect=datas.isCollect;//是否已经被收藏
            var p_collect = datas.productCount;//收藏数
            var s_phone = shopData.phoneNO;//商家电话号码
            var s_addr = shopData.address;//商家地址
            var locX = shopData.locX;//商家所在位置的精度
            var locY = shopData.locY;//商家所在位置的纬度
            var shopImg = shopData.logoImgPath;//商家图片
            var shop_id= shopData.shopId;//商品对应的商家id
            var sellerFees=datas.sellerFees;//商家认证
            var seller_len=sellerFees.length;
            var def_img_str = '<img height=400 src="'+fullpath+'" jqimg="'+fullpath+'" width=400>';
            var imgs=datas.productImgs;
            var img_len=imgs.length;
            var imgStr='<li><img src="'+def_img+'"></li>';
            
            //已收藏标志
            if(isCollect==="1"){
                $(".collect-log .img1").addClass("has-collect").removeClass("un-collect");
                $(".collect-log .count1").html("已收藏");
            }
            $(".shop-addrs span").html(s_addr);
            $(".shop-phone .phone-number").html(s_phone);
            $(".intro .name-p").html(p_name);
            $(".shop-addr-info .name-p").html(p_name);
            $(".intro .intro-detail").html(p_mark);
            $(".open-time .price").html("￥"+p_price+"/");
            $(".open-time .unit").html(p_unit);
            $(".detailsl .jqzoom").html(def_img_str);
            for(var i=0;i<img_len;i++){
                var f_path=imgs[i].add_suffix_max();//给img添加_max后缀
                imgStr+='<li><img src="'+f_path+'"></li>';        
            }
            $("#spec-list .list-h").html(imgStr);
            $(".serviceDetail-intro").html(p_mark);
            $(".contact-phone").html(s_phone);
            $(".coll-count").html(p_collect);
            $(".view-count").html(p_view);
            $(".service-level .typeName").html(b_type);
            $(".pic-header img").attr("src",shopImg);
            setMapAddr(locX,locY);//商家位置地图
            $(".shop-go").on("click",function(){//进入店铺
                location.href="./shopDetail.html?shopId="+shop_id;
            });
            
            //商家是否认证
            if(seller_len>0){

            }else{
                console.log("商家未认证");
            }
            
            //当前商家服务
            
            if(datas.sellerFees.length>0){
                var shopService=data.data.tagServices;//当前商家已开通标签
                var serviceLen=shopService.length;
                var s_tag_str='';
                var s_tag_det='';
                var more_tag='<span class="more-l">更多优惠<span class="more-lImg"></span></span>';
                if(serviceLen<3){
                    more_tag='<span class="more-l">当前商家未添加标签<span class="more-lImg"></span></span>';
                }
                for(var k=0;k<serviceLen;k++){
                    if(shopService[k].sort<4){
                        s_tag_str+='<span class="tag">'+shopService[k].name+'</span>';    
                    }else{
                        s_tag_det+='<p class="tag-detail"><img src="'+shopService[k].tagIconPath+'" class="tagImg"><span class="tag-intro">'+shopService[k].userRemark+'</span></p>';
                    }  
                }
                $(".shopService-tag").append(s_tag_str+more_tag);
                $(".tagDetail").append(s_tag_det);
            }else{
                $(".shopService-tag").append('<span class="col666">(当前商家未添加标签)</span>');
            }
            setCollect();
            //根据当前productId对应的shopId获取到商家中产品信息
            var shopDetail=JSON.stringify({
                "data":{
                    "shopId": shop_id,
                    "userId":userId
                },
                "version":"1.0",
                "code":"getShopDetail"
            });
            
            getData(url,shopDetail,call_func_pro,asyn,type);
            
            function call_func_pro(data){
                var s_pro=data.data.productList;
                var s_pro_len=s_pro.length;
                var s_pro_str='';
                var claName='';
                if(s_pro_len>1){
                    s_pro_str='<li data-id="'+s_pro[0].productId+'" class="'+claName+'">'
                            +'<div class="data_lable_div"></div>'
                            +'<img class="data_img lazy" title="'+s_pro[0].productName+'" src="./images/loading.gif" data-original="'+s_pro[0].mainImgPath+'" alt="">'
                            +'<div class="pro_det"><img src="./images/renzheng.png" alt=""><span class="pro_detail_name">'+s_pro[0].productTypeName+'</span><span class="pro_det_price">￥'+s_pro[0].saleprice+'</span></div>'
                            +'<div class="pro_mark">'+s_pro[0].productName+'</div>'
                            +'<div class="pro_detail">'
                                +'<p>'+s_pro[0].remark+'</p>'
                            +'</div>'
                        +'</li>';
                    for(var i=1;i<s_pro_len;i++){
                        if(i>0){
                            claName='marg_l10';
                        }
                        if(i==5){
                            break;
                        }
                        s_pro_str+='<li  data-id="'+s_pro[0].productId+'"  class="'+claName+'">'
                            +'<div class="data_lable_div"></div>'
                            +'<img class="data_img lazy" title="'+s_pro[i].productName+'" src="./images/loading.gif" data-original="'+s_pro[i].mainImgPath+'" alt="">'
                            +'<div class="pro_det"><img src="./images/renzheng.png" alt=""><span class="pro_detail_name">'+s_pro[i].productTypeName+'</span><span class="pro_det_price">￥'+s_pro[i].saleprice+'</span></div>'
                            +'<div class="pro_mark">'+s_pro[i].productName+'</div>'
                            +'<div class="pro_detail">'
                                +'<p>'+s_pro[i].remark+'</p>'
                            +'</div>'
                        +'</li>';
                    }
                    
                    $(".shopping .productList").html(s_pro_str+'<div class="clears"></div>');
                    $("img.lazy").lazyload({
                        threshold : 200
                    });
                }
                
            }
            
        }
        //详情信息-------------------------
        
        //获取商品类型
        getData(url,kid_p_type,call_func_k_type,asyn,type);
        /**
         *获取商品类型
         **/
        function call_func_k_type(data){
        
        }
    }

})();